package com.ruoyi.common.utils;

import com.ruoyi.common.utils.http.HttpUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class RechargeUtils {


    /**
     * 欧飞充值
     */
    public  static Map doOFPay(String gameUserId, String sporderId, String cardNum){
        String url = ConstantUtils.OUFEI_PAYURL;
        Map<String,Object> param = new HashMap<>();
        String userId = ConstantUtils.OUFEI_USER_ID;
        param.put("userid",userId);
        String userPws = MD5Util.toMD5(ConstantUtils.OUFEI_USER_PWD);
        param.put("userpws",userPws);
        String cardId = "140101";
        param.put("cardid",ConstantUtils.OUFEI_CARD_ID);
        param.put("cardnum",cardNum);
        //param.put("mctype","48"); //慢充需要传多少小时到账
        param.put("sporder_id",sporderId);
        String sporderTime = DateUtils.parseDateToStr("yyyyMMddHHmmss",new Date());
        param.put("sporder_time",sporderTime);
        param.put("game_userid",gameUserId); //18611142397 13671355890
        String userKey = ConstantUtils.OUFEI_KEY;

        StringBuffer sb = new StringBuffer();
        sb.append(userId).append(userPws).append(cardId).append(cardNum).append(sporderId).append(sporderTime).append(gameUserId).append(userKey);
        param.put("md5_str",MD5Util.toMD5(sb.toString()).toUpperCase());
        param.put("ret_url","http://47.107.117.60/api/recharge/ofCallback"); //回调地址（订单有状态了回调）
        param.put("version","6.0");
        String resultStr = HttpUtils.doPostReturnStr(url,param);
        String jsonStr = JsonUtils.xml2JSON(resultStr);
        if(StringUtils.isNotEmpty(jsonStr)){
            return JsonUtils.jsonToPojo(jsonStr,Map.class);
        }else {
            return null;
        }
    }

    /**
     * 欧飞查订单状态
     * 返回的是状态，0充值中，1成功，9失败
     * @param sporderId
     * @return
     */
    public static String getOFOrderStatus(String sporderId){
        String url = ConstantUtils.OUFEI_ORDER_STATUS_URL;
        Map<String,Object> param = new HashMap<>();
        String userId = ConstantUtils.OUFEI_USER_ID;
        param.put("userid",userId);
        param.put("spbillid",sporderId);
        String resultStr = HttpUtils.doPostReturnStr(url,param);
        return resultStr;
    }


    /**
     * 欧飞充值
     */
    public  static Map getOFOrderInfo(String sporderId){
        String url = ConstantUtils.OUFEI_ORDER_INFO_URL;
        Map<String,Object> param = new HashMap<>();
        String userId = ConstantUtils.OUFEI_USER_ID;
        param.put("userid",userId);
        String userPws = MD5Util.toMD5(ConstantUtils.OUFEI_USER_PWD);
        param.put("userpws",userPws);
        param.put("sporder_id",sporderId);
        String userKey = ConstantUtils.OUFEI_KEY;

        StringBuffer sb = new StringBuffer();
        sb.append(userId).append(userPws).append(sporderId).append(userKey);
        param.put("md5_str",MD5Util.toMD5(sb.toString()).toUpperCase());
        param.put("version","6.0");
        String resultStr = HttpUtils.doPostReturnStr(url,param);
        if(StringUtils.isEmpty(resultStr)){
            return null;
        }
        String jsonStr = JsonUtils.xml2JSON(resultStr);
        if(StringUtils.isNotEmpty(jsonStr)){
            return JsonUtils.jsonToPojo(jsonStr,Map.class);
        }else {
            return null;
        }
    }


    /**
     * 盈华充值
     * @param uid 手机号码
     * @param  sporder 系统订单号
     * @param  price 充值金额
     */
    public static Map  doYHPay(String uid,String sporder,String price){
        String url = ConstantUtils.YINGHUA_PAYURL;
        Map<String,Object> param = new HashMap<>();
        String spid = ConstantUtils.YINGHUA_SPID;
        param.put("spid", spid);
        param.put("sporder",sporder);
        param.put("price",price);
        param.put("uid",uid); //18611142397 13671355890
        String ip = "47.107.117.60";
        param.put("ip",ip);  //47.107.117.60
        String payType = "3";
        param.put("paytype",payType);
        String smallcategories = "3";
        param.put("smallcategories",smallcategories);
        String timestamp = DateUtils.parseDateToStr("yyyyMMddHHmmss",new Date());
        param.put("timestamp",timestamp);
        String flowratetype = "0";
        param.put("flowratetype",flowratetype);
        StringBuffer sb = new StringBuffer();
        sb.append(spid).append(sporder).append(price).append(uid).append(ip).append(payType).append(flowratetype).append(ConstantUtils.YINGHUA_SECRET_KEY);
        String md5key =sb.toString();
        param.put("sign",MD5Util.toMD5(md5key));
        Map resultMap   = HttpUtils.doGet(url,param);
        return resultMap;

    }



    /**
     * 获取盈华订单
     * 返回商户状态码（200：成功，202：充值中，   201：充值失败，203：参数错误，若状态码不在错误码列表内，请找人工核实订单
     */

    public static Map  getYHOrder(String sporder){
        String url = ConstantUtils.YINGHUA_ORDER_URL;
        Map<String,Object> param = new HashMap<>();
        String spid = ConstantUtils.YINGHUA_SPID;
        param.put("spid", spid);
        param.put("sporder",sporder);

        StringBuffer sb = new StringBuffer();
        sb.append(spid).append(sporder).append(ConstantUtils.YINGHUA_SECRET_KEY);
        String md5key =sb.toString();
        param.put("sign",MD5Util.toMD5(md5key));
        Map resultMap   = HttpUtils.doGet(url,param);
        return resultMap;
    }


    /**
     * 告诉商家订单充值结果
     * @param callBackUrl 商户回调地址
     * @param orderNo
     * @param code
     * @param ordersuccesstime
     * @param errorMsg
     */
    public static String doStoreCallBack(String callBackUrl,String orderNo,String sysOrderNo,String code,String ordersuccesstime,String errorMsg){
        Map param = new HashMap();
        param.put("code",code);
        param.put("msg",errorMsg);
        Map  dataMap = new HashMap();
        dataMap.put("orderID",sysOrderNo);
        dataMap.put("spOrderID",orderNo); //走老的逻辑，以前商户接收的spOrderID是商户提交充值请求时候传的订单号
        param.put("data",dataMap);
        Map map = HttpUtils.doJsonPost(callBackUrl,param);
        if(map != null){
            return JsonUtils.objectToJson(map);
        }else{
            return "商户回调失败";
        }
    }

    public static void main(String[] args) {
        Map resultMap  = null;
        //String ofOrderStatus = "";
         // resultMap = doOFPay("15815521102","D2022011911492350","30");
/*        resultMap = doYHPay("15814138689","D202201200142400","30");
       // resultMap = getYHOrder("D2022011910142311");
        //ofOrderStatus = getOFOrderStatus("D2022011911492300");
        System.out.println(resultMap.get("code"));
        System.out.println(resultMap.get("msg"));
        System.out.println(resultMap.get("data"));
        Map orderInfoMap  = JsonUtils.jsonToPojo(String.valueOf(resultMap.get("data")),Map.class);
        System.out.println(orderInfoMap.get("orderID"));*/
        //签名校验   签名 md5(充值总金额 + code + createTime + secretKey) 保住顺序
        System.out.println(MD5Util.toMD5("100Store00420220124150910MrhYlkqCFI6T4BtsYSszNQ=="));

    }

}
